package com.qigao.mall.web.controller.app.user;

import com.qigao.mall.api.AddrService;
import com.qigao.mall.commons.dto.AddrCreateDto;
import com.qigao.mall.commons.dto.AddrModiDto;
import com.qigao.mall.commons.dto.AddrResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.AddrCreateVo;
import com.qigao.mall.web.vo.app.AddrModiVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author luiz
 * @Title: com.qigao.mall.web.controller.app
 * @ProjectName three-mall
 * @date 2019-11-2019/11/5 16:17
 * @Description:
 */
@RestController
@RequestMapping("/app/user/addr")
@Api(tags = "App收件人地址API",value = "App收件人地址API")
public class AppAddrController {

    private static Logger logger= LoggerFactory.getLogger(AppAddrController.class);
    @Resource
    private AddrService addrService;
    //1.按照用户获取收件人列表
    @GetMapping("findAddrList")
    public Result<List<AddrResultDto>> findAddrList(HttpServletRequest request){
        try{
            String userNo=RequestUtils.getCurrentUserNo(request);
            return addrService.findAddrList(userNo);
        }catch (Exception e){
            logger.error("获取收件人列表发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //1.按照Id获取收件人地址信息
    @GetMapping("findAddrById/{id}")
    public Result<AddrResultDto> findAddrById(@PathVariable("id")String id){
        try{
            return addrService.findAddrById(id);
        }catch (Exception e){
            logger.error("获取收件人列表发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    //1.按照Id获取收件人地址信息
    @GetMapping("findDefaultAddr")
    public Result<AddrResultDto> findDefaultAddr(HttpServletRequest request){
        try{
            String userNo=RequestUtils.getCurrentUserNo(request);
            return addrService.findDefaultAddr(userNo);
        }catch (Exception e){
            logger.error("获取收件人默认地址发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    //1.新增收件人地址信息
    @PostMapping("/createAddr")
    @ApiOperation(value = "新增收件人地址信息", notes = "新增收件人地址信息")
    public Result<String> createAddr(@RequestBody AddrCreateVo addrCreateVo, HttpServletRequest request){
        try{
            //参数校验
            ValidatorUtil.validateEntity(addrCreateVo,AddrCreateVo.class);
            AddrCreateDto addrCreateDto=new AddrCreateDto();
            BeanUtils.copyProperties(addrCreateVo,addrCreateDto);
            addrCreateDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            addrService.createAddr(addrCreateDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("[新增收件人地址信息]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //3.修改收件人地址信息
    @PostMapping("/modiAddr")
    @ApiOperation(value = "修改收件人地址信息", notes = "修改收件人地址信息")
    public Result<String> modiAddr(@RequestBody AddrModiVo addrModiVo, HttpServletRequest request){
        try{
            //参数校验
            ValidatorUtil.validateEntity(addrModiVo,AddrModiVo.class);
            AddrModiDto addrModiDto=new AddrModiDto();
            BeanUtils.copyProperties(addrModiVo,addrModiDto);
            addrModiDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            addrService.modiAddr(addrModiDto);
            return Result.newSuccess();
        }catch (Exception e){
            logger.error("[修改收件人地址信息]发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //4.删除收件人地址信息
    @GetMapping("deleteById/{addrId}")
    public Result deleteById(@PathVariable("addrId")String addrId){
        try{
            return addrService.deleteById(addrId);
        }catch (Exception e){
            logger.error("删除收件人地址信息异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


}
